<?php

class FolhaPontoController extends Zend_Controller_Action
{

    protected $folha;

    public function init()
    {
        $this->folha = new FolhaPonto();
    }
    
    public function indexAction()
    {
        
        $this->view->data = $this->folha->getHoras('ano<=2012 AND mes<='.date('m',time()),array('ano DESC','mes DESC','nome'));
        
        $table = new tri_Db_Table('usuario');
        $this->view->lista_usuarios = $table->fetchPairs('id', 'nome');
        
    }

    public function formAction()
	{
		$id    = Zend_Filter::filterStatic($this->_getParam('id'), 'int');
		$form  = new Application_Form_FolhaPonto(); 
		
		if ($id) {
			$data = $this->folha->find($id)->current()->toArray();
			$form->populate($data);
		}
		
		$this->view->form = $form;
	}
	
	public function saveAction()
	{
		if ($this->getRequest()->isPost()) {
			$form  = new Application_Form_FolhaPonto(); 
			if ($form->isValid($this->_getAllParams())) {
				$data = $form->getValues();
				if (isset($data['id']) && $data['id']) {
					$this->folha->update($data, array('id = ?' => $data['id']));
				} else {
					$this->folha->insert($data);
				}
				$this->_redirect('default/folha-ponto/index');
			}
			
			$this->view->form = $form;
			$this->render('form');
		}
	}
	
	public function deleteAction()
	{
		$id = Zend_Filter::filterStatic($this->_getParam('id'), 'int');
		
		if ($id) {
			$this->folha->delete(array('id = ?' => $id));
		}
		
		$this->_redirect('default/folha-ponto/index');
	}
    
}
